package com.orange.quickback.system.runner;

import com.orange.quickback.system.dao.system.config.QBootConfigRepository;
import com.orange.quickback.system.dao.user.manage.QBootRoleMenuRepository;
import com.orange.quickback.system.entities.system.config.QBootConfig;
import com.orange.quickback.system.entities.user.manage.QBootRole;
import com.orange.quickback.system.entities.user.manage.QBootRoleMenu;
import com.orange.quickback.system.service.system.config.QBootConfigService;
import com.orange.quickback.system.service.user.manage.QBootUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.ArrayList;
import java.util.List;


@Component
public class QBootInit implements ApplicationRunner {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    QBootUserService qBootUserService;
    @Resource
    QBootConfigService qBootConfigService;

    @Resource
    QBootRoleMenuRepository qBootRoleMenuRepository;
    @Resource
    QBootConfigRepository qBootConfigRepository;
    public void addDBA() throws Exception{
        qBootUserService.initRegister();
    }
    public void addMenu() throws Exception{
        QBootRoleMenu qBootRoleMenu = new QBootRoleMenu("/dba/**");
        qBootRoleMenu.setPattern("/db/**");
        List<QBootRole> list = new ArrayList<>();
        list.add(new QBootRole(1L,"ROLE_dba","数据库管理员"));

        qBootRoleMenu.setQBootRoles(list);
        List<QBootRole> arr2 = new ArrayList<>();
        arr2.add(new QBootRole(2L,"ROLE_admin","系统管理员"));
        arr2.add(new QBootRole(1L,"ROLE_dba","数据库管理员"));
        QBootRoleMenu qBootRoleMenu2 = new QBootRoleMenu("/admin/**");
        qBootRoleMenu2.setQBootRoles(arr2);

        QBootRoleMenu qBootRoleMenu3 = new QBootRoleMenu("/user/**");
        List<QBootRole> arr3 = new ArrayList<>();
        arr3.add(new QBootRole(2L,"ROLE_admin","系统管理员"));
        arr3.add(new QBootRole(1L,"ROLE_dba","数据库管理员"));
        arr3.add(new QBootRole(3L,"ROLE_user","普通用户"));
        qBootRoleMenu3.setQBootRoles(arr3);

        QBootRoleMenu qBootRoleMenu4 = new QBootRoleMenu("/**");
        List<QBootRole> arr4 = new ArrayList<>();
        arr4.add(new QBootRole(4L,"ROLE_ANONYMOUS","游客"));
        qBootRoleMenu4.setQBootRoles(arr4);
        qBootRoleMenuRepository.save(qBootRoleMenu);
        qBootRoleMenuRepository.save(qBootRoleMenu2);
        qBootRoleMenuRepository.save(qBootRoleMenu3);
        qBootRoleMenuRepository.save(qBootRoleMenu4);
    }
    public void addConfig() throws  Exception{
        qBootConfigRepository.save(new QBootConfig("init_config","true","是否初始化成功"));
    }
    @Override
    public void run(ApplicationArguments args) throws Exception {
        if(qBootConfigService.isInit()) {
            addDBA();
            addMenu();
            addConfig();
        }
        log.info("超级管理员用户装填完毕！");
    }
}
